package com.atguigu.gmall.pms.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.entity.SkuEntity;
import com.atguigu.gmall.pms.mapper.AttrMapper;
import com.atguigu.gmall.pms.service.SkuService;
import com.atguigu.gmall.pms.vo.SaleAttrValueVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.SkuAttrValueMapper;
import com.atguigu.gmall.pms.entity.SkuAttrValueEntity;
import com.atguigu.gmall.pms.service.SkuAttrValueService;
import org.springframework.util.CollectionUtils;


@Service("skuAttrValueService")
public class SkuAttrValueServiceImpl extends ServiceImpl<SkuAttrValueMapper, SkuAttrValueEntity> implements SkuAttrValueService {

    @Autowired
    AttrMapper attrMapper;
    @Autowired
    SkuService skuService;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SkuAttrValueEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SkuAttrValueEntity>()
        );

        return new PageResultVo(page);
    }

    /*
    查询sku的销售属性
     */
    @Override
    public List<SkuAttrValueEntity> querySearchAttrValueByCidAndSkuId(Long cid, Long skuId) {
        //根据分类查询销售属性
        List<AttrEntity> attrEntities = attrMapper.selectList(new QueryWrapper<AttrEntity>().eq("search_type", 1).
                eq("category_id", cid).
                eq("type", 0));
        if (CollectionUtils.isEmpty(attrEntities)){
            return null;
        }
        List<Long> longs = attrEntities.stream().map(AttrEntity::getId).collect(Collectors.toList());
        List<SkuAttrValueEntity> list = this.list(new QueryWrapper<SkuAttrValueEntity>().eq("sku_id", skuId).in("attr_id", longs));
        return list;
    }

    /*
    查询spu下的所有销售组合
     */
    @Override
    public List<SaleAttrValueVo> querySkuSaleAtrrBySpuId(Long spuId) {
        //首先查询spu下的所有skuid
        List<SkuEntity> list = skuService.list(new QueryWrapper<SkuEntity>().eq("spu_id", spuId).select("id"));
        if (CollectionUtils.isEmpty(list)){
            return null;
        }
        List<Long> collect = list.stream().map(SkuEntity::getId).collect(Collectors.toList());
        //获取sku的销售属性
        List<SkuAttrValueEntity> skuAttrValueEntities = this.baseMapper.selectList(new QueryWrapper<SkuAttrValueEntity>().in("sku_id", collect));
        if (CollectionUtils.isEmpty(skuAttrValueEntities)){
            return null;
        }
        //根据销售id分组 分组 k->AttrId
        Map<Long, List<SkuAttrValueEntity>> listMap = skuAttrValueEntities.stream().collect(Collectors.groupingBy(SkuAttrValueEntity::getAttrId));
        List<SaleAttrValueVo> saleAttrValueVos = new ArrayList<>();
        listMap.forEach((attrId,skuAttrValueEntity)->{
            SaleAttrValueVo saleAttrValueVo = new SaleAttrValueVo();
            saleAttrValueVo.setAttrId(attrId);
            saleAttrValueVo.setAttrName(skuAttrValueEntity.get(0).getAttrName());
            saleAttrValueVo.setAttrValues(skuAttrValueEntity.stream().map(SkuAttrValueEntity::getAttrValue).collect(Collectors.toSet()));
            saleAttrValueVos.add(saleAttrValueVo);
        });
        return saleAttrValueVos;
    }

    /*
    查询sku下的销售属性和skuid的映射关系
     */
    @Override
    public String queryMappingBySpuId(Long spuId) {
        //查询所有sku
        List<SkuEntity> list = skuService.list(new QueryWrapper<SkuEntity>().eq("spu_id", spuId).select("id"));
        if (CollectionUtils.isEmpty(list)){
            return null;
        }
        List<Long> skuIds = list.stream().map(SkuEntity::getId).collect(Collectors.toList());
        List<Map<String,Object>> attrs = attrMapper.queryMappingBySkuId(skuIds);
        if (CollectionUtils.isEmpty(attrs)){
            return null;
        }
        Map<String, Long> collect = attrs.stream().collect(Collectors.toMap(map -> map.get("attr_values").toString(), map -> (Long) map.get("sku_id")));
        String jsonString = JSON.toJSONString(collect);
        return jsonString;
    }

}